import aead
import urandom

_key = b'\xf9\xd7\xde\xe9\xa9\xcb\xbd\xec\xbf\xf8\x8a\xa7\x78\xb5\x99\xb2\x8c\x1a\xdf\xd6\x26\x28\x92\x3a\xae\xe4\x76\xc5\xec\x2f\x38\x1c'
_ad = b'\xce\x86\x81\x95\x3d\x44\x63\x51\xac\xb8\x45\x39'

class PWSCrypt:
	def __init__(self):
		aead.init(_key, _ad)

	def encrypt(self, data):
		# 2(addr) + 8(random) + 16(tag) + data len
		buf = bytearray(10)
		buf[0] = 0x01
		buf[1] = 0x00

		for i in range(2, 10):
			buf[i] = urandom.randrange(0, 255)

		tag = bytearray(16)
		aead.encrypt(data, buf[2:10], tag)
		buf += tag
		buf += data
		
		return buf

	def decrypt(self, buf):
		buflen = len(buf)
		data = bytearray(buf[26:buflen])
		ret = aead.decrypt(data, buf[2:10], buf[10:26])
		return ret, data